﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Xml;
using System.IO;
using System.Xml.XPath;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;

namespace XMLVisualizer
{
    public class Startup
    {
        [STAThread]
        public static void Main(string[] args)
        {
            SingleInstanceApplicationWrapper wrapper = new SingleInstanceApplicationWrapper();
            wrapper.Run(args);
        }
    }

    public class SingleInstanceApplicationWrapper :
        Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase
    {
        public SingleInstanceApplicationWrapper()
        {
            this.IsSingleInstance = true;
        }

        // Create the WPF application class.
        private MainApp app;

        protected override bool OnStartup(Microsoft.VisualBasic.ApplicationServices.StartupEventArgs e)
        {
            //string extension = ".testDoc";
            //string title = "SingleInstanceApplication";
            //string extensionDescription = "A Test Document";
            // Uncomment this line to create the file registration.
            // In Windows Vista, you'll need to run the application
            // as an administrator.            
            //FileRegistrationHelper.SetFileAssociation(
            //  extension, title + "." + extensionDescription);
            app = new MainApp();

            ResourceDictionary resourceDictionary = new ResourceDictionary();
            resourceDictionary.Source = new Uri("pack://application:,,,/ResourceDictionary.xaml");
            app.Resources = resourceDictionary;

            //app.StartupUri = new Uri("Document.xaml", System.UriKind.Relative);
            app.Run();

            return false;
        }

        // Direct multiple instances
        protected override void OnStartupNextInstance(Microsoft.VisualBasic.ApplicationServices.StartupNextInstanceEventArgs e)
        {
            if (e.CommandLine.Count > 0)
            {
                app.ShowDocument(e.CommandLine[0]);
            }
        }
    }

    // This package contains a reference to a document window and its name.
    // The purpose of this class is to make it easier to display the list
    // of window names in DocumentList through data binding.
    public class DocumentReference
    {
        private Document document;
        public Document Document
        {
            get { return document; }
            set { document = value; }
        }

        private string name;
        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public DocumentReference(Document document, string name)
        {
            Document = document;
            Name = name;
        }
    }

    public class MainApp : System.Windows.Application
    {
        protected override void OnStartup(System.Windows.StartupEventArgs e)
        {
            base.OnStartup(e);

            // Load the document that was specified as an argument.
            if (e.Args.Length > 0)
            {
                try
                {
                    Document doc = new Document();
                    this.MainWindow = doc;
                    DocumentReference docRef = new DocumentReference(doc, e.Args[0]);
                    doc.LoadFile(docRef);
                    doc.Show();
                    doc.Activate();
                    Documents.Add(docRef);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(/*ex.StackTrace*/ex.Message, "Error", MessageBoxButton.OK, MessageBoxImage.Error);
                }
                //catch
                //{
                //    MessageBox.Show("Could not load document.");
                //}
            }
            else
            {
                Document doc = new Document();
                this.MainWindow = doc;
                doc.Show();
            }
        }

        // An ObservableCollection is a List that provides notification
        // when items are added, deleted, or removed. It's preferred for data binding.
        private ObservableCollection<DocumentReference> documents = new ObservableCollection<DocumentReference>();
        public ObservableCollection<DocumentReference> Documents
        {
            get { return documents; }
            set { documents = value; }
        }

        public void ShowDocument(string filename)
        {
            try
            {
                Document doc = new Document();
                DocumentReference docRef = new DocumentReference(doc, filename);
                doc.LoadFile(docRef);
                //doc.Owner = this.MainWindow;
                doc.Show();
                doc.Activate();
                Documents.Add(docRef);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Could not load document.\n" + ex.Message);
                MessageBox.Show(ex.StackTrace);
            }
        }
    }
}